package com.qk.appnewslist;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by user on 2016/9/5.
 */
public class TitleFragment extends Fragment {
    ListView listView;
    View view;
    private ArrayList<NewsInfo> newsList;
    NewsInfo news;
    ArrayList<HashMap<String, Object>> listItem;
    boolean mDualPane;//是否是横屏
    int mCurCheckPosition = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.titlefragment, container, false);
        listView=(ListView)view.findViewById(R.id.lv_news_list);
        newsList=new ArrayList<NewsInfo>();
        for(int i=0;i<10;i++)
        {
            news= new NewsInfo();
            news.setNewsName("新闻 "+i);
            news.setNewsContent("这是新闻内容，这是新闻内容，这是新闻内容，这是新闻内容，这是新闻内容，这是新闻内容，这是新闻内容，这是新闻内容，这是新闻内容，这是新闻内容"+i);
            newsList.add(news);
        }

        listItem = new ArrayList<HashMap<String, Object>>();
        for(NewsInfo n : newsList)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("NewsTitle", n.getNewsName());
            map.put("NewsText", n.getNewsContent());
            listItem.add(map);
        }
        //生成适配器的Item和动态数组对应的元素
        SimpleAdapter listItemAdapter = new SimpleAdapter(
                getActivity(),
                listItem, //数据源
                R.layout.news_item, //ListItem的XML实现
                new String[]{"NewsTitle"},//动态数组与listItem对应的子项
                new int[]{R.id.tv_title}
        );
        //添加并且显示
        listView.setAdapter(listItemAdapter);
        //点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showDetails(position);
            }
        });

        return view;
    }

    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        View detailsFrame = getActivity().findViewById(R.id.details);
        mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
        if (savedInstanceState != null) {
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }

        if (mDualPane) {
            listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            showDetails(mCurCheckPosition);
        }
    }

    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

    public void showDetails(int index){
        mCurCheckPosition = index;
        Bundle bundle=new Bundle();
        bundle.putInt("index", index);
        bundle.putString("title",newsList.get(index).getNewsName());
        bundle.putString("text",newsList.get(index).getNewsContent());

        //横屏
        if (mDualPane){
            listView.setItemChecked(index,true);
            //如果第二次点击，则不重新replace
            DetailsFragment details = (DetailsFragment) getFragmentManager().findFragmentById(R.id.details);
            if (details == null || details.getShownIndex() != index){
                details = DetailsFragment.newInstance(index);
                details.setArguments(bundle);
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.details, details);
                ft.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }
        }
        //竖屏
        else {
            Intent intent = new Intent(getActivity(), DetailsActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    }
}
